package com.example.android.ui.message;

import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.android.R;
import com.example.android.bean.BaseBean;
import com.example.android.bean.Message;
import com.example.android.ui.base.BaseFragment;
import com.example.android.ui.message.Adapter.MessageRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends BaseFragment implements MessageContract.IMessageView, SwipeRefreshLayout.OnRefreshListener, MessageRecyclerViewAdapter.OnItemClickListener {
    private List<Message> messageList;
    private MessagePresenter messagePresenter;
    private MessageRecyclerViewAdapter messageAdapter;
    private SwipeRefreshLayout messageSwipeRefresh;
    @Override
    protected void initViews() {
        messageSwipeRefresh = find(R.id.message_swipeRefresh);
        messageSwipeRefresh.setOnRefreshListener(this);
        RecyclerView recyclerView = find(R.id.message_recyclerView);
        messageList =new ArrayList<>();
        messageAdapter = new MessageRecyclerViewAdapter(recyclerView,getActivity(),messageList);
        messageAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(messageAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        messagePresenter = new MessagePresenter(this);
        messagePresenter.getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }


    @Override
    public void getMessageSuccess(BaseBean<List<Message>> messageList) {
        messageAdapter.setMessageList(messageList.getData());
    }

    @Override
    public void getMessageError(Throwable throwable) {

    }

    @Override
    public void onRefresh() {
        messageSwipeRefresh.setRefreshing(false);
        messagePresenter.getData();
    }

    @Override
    public void onItemClick(Message message) {
        Log.i("MessageActivity","点击item" + message.getUserId());
    }
}
